
#include <typedefs.h>
#include <pcb_defs.h>
#include <i_op.h>
#include <iomap.h>
#include <config.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <zone_status.h>
#include <status.h>
#include <sig_types.h>
#include <sig_ui.h>
#include <detector.h>
#include <p_status.h>
#include <det_ui.h>
#include <exception.h>
#include <glp.h>
#include <err_handler.h>
#include <p_ui.h>

#include <sigpath.h>

BIT8 tempLabel[10] = "DetUnit";

ON_OFF   GetDetOnOff (POSITION p)
{
   if (GetDetectorType(p) == LDID) return GetLDIDElecOnOff(p);
   else
   {
        /* Return the bit value of the hardware image */
        if (p == FRONT)
        {
                return (DiscreteOut1_Image & DETA_BD_ON) ? ON : OFF;
        }
        else
        {
                return (DiscreteOut1_Image & DETB_BD_ON) ? ON : OFF;
        }
   }
}
ON_OFF GetLDIDElecOnOff(POSITION p)
{
   /* Return the bit value of the hardware image */
   if (p == FRONT)
   {
      return ((DiscreteOut1_Image & DETA_BD_ON) && (DiscreteOut2_Image & DETA_INPUT1_ON)) ? ON : OFF;
   }
   else
   {
      return ((DiscreteOut1_Image & DETB_BD_ON) && (DiscreteOut2_Image & DETB_INPUT1_ON)) ? ON : OFF;
   }
}

ON_OFF   GetDetDiagLinearMode(POSITION p)
{
  printf("call GetDetDiagLinearMode\n");
  return 0;
}
DET_TYPE GetDetectorType(POSITION p)
{
  return (p == FRONT) ? DetectorA_Config.type : DetectorB_Config.type;
}
INT32 GetDetectorOffset(POSITION p)
{
        /* scale to match SIGNAL display */
        return ((p == FRONT) ? Deta_Data : Detb_Data) / SIG_DISP_SCALE_FACTOR;
}

BIT8     GetCPFDetUnitsLabelPresence( POSITION position )
{
  printf("call GetCPFDetUnitsLabelPresence\n");
  return 0;
}

BIT8    *GetCPFDetUnitsLabel( POSITION position )
{
  printf("call GetCPFDetUnitsLabel\n");
  return tempLabel;
}
BIT8   *GetDetGasLabel( POSITION pos, BIT8 gas )
{
  printf("call GetDetGasLabel\n");
  return tempLabel;
}
UI_ERR StartNPDOffsetSeek(POSITION p)
{
  printf("call StartNPDOffsetSeek\n");
  return OK;
}
UI_ERR StartECDOffsetSeek(POSITION p)
{
  printf("call StartECDOffsetSeek\n");
  return OK;
}
void SetDetDefaultSetpts(const void *p)
{
  printf("call SetDetDefaultSetpts\n");
}
void DetGasTypeChanged(POSITION p)
{
        switch (GetDetectorType(p))
        {
/* REVISIT: do we support this for uECD? */
        case uECD:
        case ECD:
                /* Inform ECD of current gas type */
                /* Note:  the narrow pulse width is only enabled for ArMe.  If
                          the gas type is set to any other or is undefined then
                          use the wider N2 pulse width. */
                switch(DetGasType(p))
                {
                case ArMe:
                        SetDetInput2(p, OFF);   /* Ar/Me = narrow pulse width */
                        break;
                default:
                        SetDetInput2(p, ON);    /* N2 = wide pulse width */
                        break;
                }
                break;
        case TCD:
                /* Inform TCD of current gas type */
                /* Note:  the H2/He setting is used if the gas type is unspecified:
                          H2/He is used for most serious TCD work, and that choice
                          also sets the on-board relay to the de-energized state. */
                switch(DetGasType(p))
                {
                case H2:
                case He:
                case UNKNOWN_GAS:
                        SetDetInput2(p, OFF);   /* H2/He/unspecified */
                        break;
                default:
                        SetDetInput2(p, ON);    /* other */
                        break;
                }
                break;
        }
}
INT16 DetectorIsOn(POSITION p)
{
  printf("call DetectorIsOn\n");
  return 0;
}
BIT8 ManualElectrometer( POSITION p )
{
  printf("call ManualElectrometer\n");
  return 1;
}
BIT8 GetCPFDetPneuPresence( POSITION position )
{
   if (position==FRONT)
   {
      return (FDCPF);
   }
   else
   {
      return (BDCPF);
   }
}
INT16 IsDetGasRequired(POSITION p, INT8 gas)
{
//  printf("call IsDetGasRequired\n");
  return 0;
}
OEM_GAS_STATUS  GetOEMGasStatus( pDETECTOR_STATUS status, BIT8 gas )
{
  printf("call GetOEMGasStatus\n");
  return NOT_USED;
}
BIT8   GetElectrometerType( POSITION p )
{
  printf("call GetElectrometerType\n");
  return 0;
}
BIT8 GetCPFDetTitlePresence( POSITION position )
{
  printf("call GetCPFDetTitlePresence\n");
  return 0;
}
BIT8   *GetCPFDetTitle( POSITION position )
{
  printf("call GetCPFDetTitle\n");
  return tempLabel;
}
BIT8 GetCPFDetGasPresence( POSITION position, BIT8 gas_num )
{
  printf("call GetCPFDetGasPresence\n");
  return 0;
}
BIT8 GetCPFOIMPresence( POSITION position )
{
  printf("call GetCPFOIMPresence\n");
  return 0;
}
BIT8 GetCPFCtlNumBits( POSITION position, BIT8 channel )
{
  printf("call GetCPFCtlNumBits\n");
  return 0;
}
BIT8 GetOEMCtlPresence( POSITION position, BIT8 channel )
{
  printf("call GetOEMCtlPresence\n");
  return 0;
}
BIT8 GetOEMCtlPulseLen( POSITION position, BIT8 channel )
{
  printf("call GetOEMCtlPulseLen\n");
  return 0;
}
void UpdateCPFCtlValues(void)
{
  printf("call UpdateCPFCtlValues\n");
}
void UpdateDetStatus(void)
{
  printf("call UpdateDetStatus\n");
}
void CheckDetConfig(void)
{
  printf("call CheckDetConfig\n");
}
void CheckCPFDetConfigInWkfile(void)
{
  printf("call CheckCPFDetConfigInWkfile\n");
}
void CheckOEMDetConfigInWkfile(void)
{
  printf("call CheckOEMDetConfigInWkfile\n");
}
void DetGasShutdown(POSITION p, INT8 gas)
{
  printf("call DetGasShutdown Position:%d gas:%d\n",p,gas);
}
ON_OFF GetTCDFilament(POSITION p)
{
  printf("call GetTCDFilament\n");
  return OFF;
  
}
void SetDetOnOff(POSITION p, ON_OFF on_off)
{
  printf("call SetDetInput1\n");
}

UI_ERR SetDetSetptOnOff(POSITION p, ON_OFF on_off)
{
  printf("call SetDetSetptOnOff\n"); 
        return OK;      /* always OK */
}
void SetDetInput1(POSITION p, ON_OFF on_off)
{
  printf("call SetDetInput1\n");
}
void SetDetDiagInput1(POSITION p, ON_OFF on_off)
{
  printf("call SetDetDiagInput1\n");
}
void SetDetInput2(POSITION p, ON_OFF on_off)
{
  printf("call SetDetInput2\n");
}
void SetDetDiagInput2(POSITION p, ON_OFF on_off)
{
  printf("call SetDetDiagInput2\n");
}
void SetDetInput3(POSITION p, ON_OFF on_off)
{
  printf("call SetDetInput3\n");
}
void SetDetDiagInput3(POSITION p, ON_OFF on_off)
{
  printf("call SetDetDiagInput3\n");
}
void SetDetInput4(POSITION p, ON_OFF on_off)
{
  printf("call SetDetInput4\n");
}
void SetDetInput5(POSITION p, ON_OFF on_off)
{
  printf("call SetDetInput5\n");
}
INT32 GetNPDTargetOffset(POSITION p)
{
        return Active_Wkfile.det_target_offset[(int)p];
}
INT32 GetNPDBeadDACVolts(POSITION p)
{
        return DetSerial[(int)p] ;
}
UI_ERR SetNPDBeadVoltage(POSITION p, INT32 value)
{
  printf("call SetNPDBeadVoltage\n");
        return OK;
}
INT32 ECDDACToRefCurrent(INT32 value)
{
        INT32 current;

        /* "half-round" because of division truncation */
        current = ((value * 10798) + (255 * 10)) / (2 * 255 * 10);

        if (current < ECD_REF_CURRENT_MIN)
        {
                return ECD_REF_CURRENT_MIN;
        }
        else if (current > ECD_REF_CURRENT_MAX)
        {
                return ECD_REF_CURRENT_MAX;
        }
        else
        {
                return current;
        }
}

static const SETPT REF_CURRENT[2] = { F_DET_REF_CURRENT, B_DET_REF_CURRENT };
UI_ERR SetECDDAC(POSITION p, INT32 value)
{
        if (value < ECD_DAC_MIN)
        {
                return PARAM_TOO_SMALL;
        }
        else if (value > ECD_DAC_MAX)
        {
                return PARAM_TOO_LARGE;
        }
        else
        {
                INTERRUPTS_OFF
                DetSerial[(int)p] = value;
                DetUserSerial[(int)p] = ECDDACToRefCurrent(value);
                INTERRUPTS_ON

                LogSetptChange( (SETPT)REF_CURRENT[(int)p], DetUserSerial[(int)p], 0 );

                return OK;
        }
}
INT32 GetECDTargetOffset(POSITION p)
{
        return Active_Wkfile.det_target_offset[(int)p];
}

static const SETPT TARGET_OFFSET[2] = { F_DET_TARGET_OFFSET, B_DET_TARGET_OFFSET };
UI_ERR SetECDTargetOffset(POSITION p, INT32 value)
{
        U_INT16 old;

        if (value < ECD_TARGET_MIN)
        {
                return PARAM_TOO_SMALL;
        }
        else if (value > ECD_TARGET_MAX)
        {
                return PARAM_TOO_LARGE;
        }
        else
        {
                old = Active_Wkfile.det_target_offset[(int)p];
                Active_Wkfile.det_target_offset[(int)p] = (U_INT16)value;

                /*
                ** Initiate an offset seek if (new value != old value), but
                ** don't do it if any dependent setpoint is wrong, because we
                ** don't want to change the dependent setpoints on a method load!
                **
                ** The keyboard separately calls StartECDOffsetSeek() to always
                ** initiate an offset seek after every entry of a target offset.
                */
                if ((value != old) &&
                    (GetDetOnOff(p) != OFF))
                {
                        (void)StartECDOffsetSeek(p);
                }

                LogSetptChange( (SETPT)TARGET_OFFSET[(int)p], value, 0 );

                return OK;
        }
}
INT32 GetECDDAC(POSITION p)
{
        return DetSerial[(int)p];
}
